package cn.com.dhcc.turbo.service.monitor.gather;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.base.IocBean;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.YESNO;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.turbo.entity.monitor.CrossNetMonitor;
import cn.com.dhcc.turbo.entity.monitor.QuotaValueOnTime;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.exch.cmdata.GapInfoService;
import cn.com.dhcc.turbo.service.monitor.CrossNetMonitorService;
import cn.com.dhcc.turbo.service.monitor.MonitorService;
import cn.com.dhcc.turbo.service.monitor.QUOTA;
import cn.com.dhcc.turbo.service.registry.BaseInfoService;

/**
 * 网闸相关指标的监控
 * @author qiao
 * @2014-9-12 @下午4:15:39
 */
@Service
public class GapQuotaGatherServer {

	@SuppressWarnings("deprecation")
	Logger logger = Log.getExistLogger(LOGTYPE.MONITOR);
	
	@Autowired
	@Qualifier(IocBean.BASE_INFO_SERVICEIMPL_BEAN)
	BaseInfoService baseInfoService;
	@Autowired
	GapInfoService gapInfoService;
	@Autowired
	MonitorService monitorService;
	@Autowired
	CrossNetMonitorService crossNetMonitorService;
	/**
	 * 	网闸指标收集
	 */
	public void gatherGapStatus(){
		QUOTA quota = QUOTA.GAP_STATUS;
		try {
			NodeInfo localNode = baseInfoService.getCurrentNodeInfo();
			if(localNode==null){
				logger.error("指标["+quota.getDesc()+"]值收集失败：节点未初始化...");
				return;
			}
			if(!"1".equals(localNode.getHasGap())){
				return;
			}
			CrossNetMonitor monitor=crossNetMonitorService.getMonitor();
			logger.info("开始收集指标["+quota.getDesc()+"]...");
			Date now = new Date();
			
			
			QuotaValueOnTime onTime = new QuotaValueOnTime();
			if ("1".equals(localNode.getHasGap())) {
				//网闸状态
				onTime.setCanMerge(YESNO.YES.getStatus());
				onTime.setCurrentValue1(monitor.getGapStatus());
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setMtime(now);
				onTime.setQuotaCode(quota.getCode());
				onTime.setOwnerId(localNode.getId());
				monitorService.insertOrUpdateQuotaOnTime(onTime);
				logger.info("指标["+quota.getDesc()+"]值收集结束，值为："+monitor.getGapStatus());
				
				//网闸待发送量
				quota = QUOTA.GAP_TO_SEND_NUM;
				onTime = new QuotaValueOnTime();
				onTime.setCanMerge(YESNO.YES.getStatus());
				onTime.setCurrentValue1(monitor.getGapToSendNum()==null ? "0":monitor.getGapToSendNum());
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setMtime(now);
				onTime.setQuotaCode(quota.getCode());
				onTime.setOwnerId(localNode.getId());
				monitorService.insertOrUpdateQuotaOnTime(onTime);
				logger.info("指标["+quota.getDesc()+"]值收集结束，值为："+monitor.getGapToSendNum());
				
				//网闸待发送大小
				quota = QUOTA.GAP_TO_SEND_SIZE;
				onTime = new QuotaValueOnTime();
				onTime.setCanMerge(YESNO.YES.getStatus());
				onTime.setCurrentValue1(monitor.getGapToSendSize());
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setMtime(now);
				onTime.setQuotaCode(quota.getCode());
				onTime.setOwnerId(localNode.getId());
				monitorService.insertOrUpdateQuotaOnTime(onTime);
				logger.info("指标["+quota.getDesc()+"]值收集结束，值为："+monitor.getGapToSendSize());
			}
			//待刻盘量
			quota = QUOTA.CARVED_DISCS_SUM;
			onTime = new QuotaValueOnTime();
			onTime.setCanMerge(YESNO.YES.getStatus());
			onTime.setCurrentValue1(monitor.getCarvedDiscsSum());
			onTime.setId(UUIDGenerator.getUUID());
			onTime.setMtime(now);
			onTime.setQuotaCode(quota.getCode());
			onTime.setOwnerId(localNode.getId());
			monitorService.insertOrUpdateQuotaOnTime(onTime);
			logger.info("指标["+quota.getDesc()+"]值收集结束，值为："+monitor.getCarvedDiscsSum());
			//待刻盘大小
			quota = QUOTA.CARVED_DISS_SIZE;
			onTime = new QuotaValueOnTime();
			onTime.setCanMerge(YESNO.YES.getStatus());
			onTime.setCurrentValue1(monitor.getCarvedDissSize());
			onTime.setId(UUIDGenerator.getUUID());
			onTime.setMtime(now);
			onTime.setQuotaCode(quota.getCode());
			onTime.setOwnerId(localNode.getId());
			monitorService.insertOrUpdateQuotaOnTime(onTime);
			logger.info("指标["+quota.getDesc()+"]值收集结束，值为："+monitor.getCarvedDissSize());
		} catch (Exception e) {
			logger.error("指标["+quota.getDesc()+"]值收集失败",e);
		}
	}
}
